package com.firefightingiot.firefightingiot.config.scheduling;

import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.support.utils.DayUtil;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import com.firefightingiot.firefightingiot.web.service.AlarmService;
import com.firefightingiot.firefightingiot.web.service.HomeDataService;
import com.firefightingiot.firefightingiot.web.service.HomePageDataService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;

@Component
public class AlarmLogScheduling {

    @Resource
    public MyLogger logger;

    @Resource
    HomeDataService homeDataService;
    @Resource
    HomePageDataService homePageDataService;
    @Resource
    AlarmService alarmService;
    @Scheduled(cron="0 10 0 * * ?")
    public void insertLogStatistics() throws InterruptedException {
        //生成昨天的统计记录
        //homeDataService.insertLogStatisticsForToday();
        homePageDataService.insertLogStatisticsForToday();
        //月数据统计
        homePageDataService.insertMonthLogStatisticsForToday();
        JSONObject jsonObject = new JSONObject();
        //删除月统计数据
        jsonObject.put("monthStartDate",DayUtil.getPastDate(30));
        homeDataService.deleteDataForStatisticsMonth(jsonObject);
        //赋值alldate,删除周统计数据：
        jsonObject.put("alldate",DayUtil.getPastDate(7));
        homeDataService.deleteDataForStatisticsWeek(jsonObject);
    }

    @Scheduled(cron="0 0 1 * * ?")//每晚1点删除今日表中的无用的记录：昨天的记录
    public void insertLogStatisticsForMonth() throws InterruptedException {
        JSONObject jsonObject = new JSONObject();
        //获取昨天的日期
        jsonObject.put("alarmtime",DayUtil.getYestoday());
        homeDataService.deleteTodayLog(jsonObject);
    }

    /**
     * @Author YangYu
     * @Description 批量更新可视化设备状态（每30分钟执行一次）
     * @Date Created in 9:31 2022/6/22
     */
    @Scheduled(cron="0 */30 * * * ?")
    public void updateVisualizationDeviceState() throws ParseException {
        alarmService.updateVisualizationDeviceState();
    }

}
